<?php

/**
 * @author dungdhs@gmail.com
 * @copyright 2012
 */

function homepage_task(){
  return array(
    'default' => array(
      'func'  => '_homepage_default',
      'perm'  => 'logged user',
			'title'	=> 'Trang chủ'
		),
		
		'newsfollow' => array(
      'func'  => '_homepage_newsfollow',
      'perm'  => 'logged user',
			'title'	=> 'Các tin quan tâm',
			'alias'	=> 'quan-tam'
		)
  );
}

function _homepage_default(){
	global $smarty, $database, $user;
	$aNews = array();
	
	$res = $database->db_query("SELECT * FROM news WHERE news_ispinned=1 AND news_status=1 AND news_published=1 ORDER BY news_updated DESC");
	while ($news = $database->db_fetch_assoc($res)){
		$aNews[] = $news;
	}
	
	$aCatId = $user->userCatId();
	$res = $database->db_query("SELECT * FROM news WHERE news_cat_id IN (%s) AND news_ispinned=0 AND news_status=1 AND news_published=1 ORDER BY news_updated DESC", implode(',', $aCatId));
	while ($news = $database->db_fetch_assoc($res)){
		$aNews[] = $news;
	}
	
	$smarty->assign('aNews', $aNews);
	$smarty->assign('aCat', __category_load('news'));
	$smarty->assign('u', $user->user_info);
	PGTheme::message_html();
	
	PGTheme::$pageTemplate = 'nowrap';
	
	__news_follower();
	__news_like();
	
	return $smarty->fetch('homepage.tpl'); 
}

function _homepage_newsfollow(){
	global $database, $user, $smarty;
	
	$aNewsFollow = __news_follower();
	$aNews = array();
	if (count($aNewsFollow)>0){
		$res = $database->db_query("SELECT * FROM news WHERE news_id IN (%s) AND news_status=1 AND news_published=1 ORDER BY news_updated DESC", implode(',', $aNewsFollow));
		while ($news = $database->db_fetch_assoc($res)){
			$aNews[] = $news;
		}
	}
	
	$smarty->assign('aNews', $aNews);
	$smarty->assign('aCat', __category_load('news'));
	$smarty->assign('u', $user->user_info);
	PGTheme::message_html();	
	__news_like();
	
	PGTheme::$pageTemplate = 'nowrap';
	
	return $smarty->fetch('homepage.tpl'); 
}
?>
